<?php
namespace common\queue;

use common\models\CacheKeys;
use common\models\friend\Friend;
use common\models\Functions;
use common\models\group\Group;
use common\models\group\GroupUser;
use common\models\im\Im;
use common\models\Status;
use common\models\user\User;
use yii\base\Exception;
use yii\queue\Job;
use Yii;

class RedPacketMessage implements Job {

    public $data = [];
    public $redis;
    public function __construct($args)
    {
        $this->data = $args;
        $this->redis = Yii::$app->redis;
    }

    public function execute($queue)
    {

        file_put_contents('/tmp/redpacket_message.txt',print_r($this->data,true),FILE_APPEND);
        try {
            $redPacketLogInfo = $this->data['redPacketLog'];
            $redPacketInfo = $this->data['redPacket'];
            $userInfoArray = $this->data['user'];
            $surplusNum = $this->data['num'];

            $imModel = new Im();

            $sendUser = User::getOneInfoById($redPacketInfo['user_id'],['nick_name','tx_code','id']);

            $sendNickName = $receiveNickName = '';
            $sendNickName = $sendUser['nick_name'];
            $receiveNickName = $userInfoArray['nick_name'];

            // 判断是否是好友  需要好友备注
            $sendFriend = Friend::getOneByWhere(['user_id' => $userInfoArray['id'],'friend_id' => $sendUser['id']],['user_id','friend_id','mark']);
            $receiveFriend = Friend::getOneByWhere(['user_id' => $sendUser['id'],'friend_id' => $userInfoArray['id']],['user_id','friend_id','mark']);


            $messageData = [];
            $messageData['message_type'] = (int) Status::MESSAGE_TYPE_GROUP_USER_RECEIVE_RED_PACKET;
            $messageData['red_packet_id'] = (int) $redPacketInfo['id'];
            $messageData['type'] = (int) Status::MESSAGE_TYPE_GROUP_USER_RECEIVE_RED_PACKET;
            $messageData['content'] = "";
            $messageData['group_tx_code'] = "";
            $messageData['tx_code'] = "";
            $messageData['time'] = date('Y-m-d H:i:s');


            if ($redPacketInfo['type'] == Status::RED_PACKET_TYPE_GROUP) {
                // 群红包
                $messageData['message_type'] = (int) Status::MESSAGE_TYPE_GROUP_USER_RECEIVE_RED_PACKET;
                $messageData['red_packet_id'] = (int) $redPacketInfo['id'];
                $messageData['type'] = (int) Status::MESSAGE_TYPE_GROUP_USER_RECEIVE_RED_PACKET;
                $messageData['content'] = "";
                $messageData['group_tx_code'] = "";
                $messageData['tx_code'] = "";
                $messageData['time'] = date('Y-m-d H:i:s');

                $groupId = $redPacketInfo['group_id'];
                $appGroupInfo = Group::getOneByWhere(['id' => $groupId],['id','tx_code']);
//                $appGroupInfo = $this->redis->get(CacheKeys::GROUP_MESSAGE."{$groupId}");
//                $appGroupInfo = json_decode($appGroupInfo,true);

//                if (empty($sendFriend) || empty($sendFriend['beizhu'])) {
//                    $groupUser = $this->redis->get(CacheKeys::GROUP_USER_MESSAGE."{$groupId}:{$sendUser['id']}");
//                    $groupUser = json_decode($groupUser,true);
//
//                    $sendNickName = $groupUser['qunmingpian'];
//                } else {
//                    $sendNickName = $sendFriend['beizhu'];
//                }
//
//                if (empty($receiveFriend) || empty($receiveFriend['beizhu'])) {
//                    $groupUser = $this->redis->get(CacheKeys::GROUP_USER_MESSAGE."{$groupId}:{$userInfoArray['id']}");
//                    $groupUser = json_decode($groupUser,true);
//                    $receiveNickName = $groupUser['qunmingpian'];
//                } else {
//                    $receiveNickName = $receiveFriend['beizhu'];
//                }


                // 显示群昵称 或者 好友备注 领取了红包
                if (empty($sendFriend) || empty($sendFriend['mark'])) {
                    $groupUser = GroupUser::getOneByWhere(['group_id' => $groupId,'user_id' => $sendUser['id']],['group_id','user_id','business_card']);
                    $sendNickName = $groupUser['business_card'] ?: $sendNickName;
                } else {
                    $sendNickName = $sendFriend['mark'];
                }

                if (empty($receiveFriend) || empty($receiveFriend['mark'])) {
                    $groupUser = GroupUser::getOneByWhere(['group_id' => $groupId,'user_id' => $userInfoArray['id']],['group_id','user_id','business_card']);
                    $receiveNickName = $groupUser['business_card'] ?: $receiveNickName;
                } else {
                    $receiveNickName = $receiveFriend['mark'];
                }


                if ($userInfoArray['id'] != $sendUser['id']) {
                    // 领取人信息
                    $messageData['content'] = "您成功领取了{$sendNickName}的红包！";
                    $messageData['group_tx_code'] = $appGroupInfo['tx_code'];
                    $messageData['tx_code'] = $userInfoArray['tx_code'];
                    $imModel->sendGroupMessage($appGroupInfo['tx_code'], $messageData);
//                    $imModel->sendGroupSingleSystemMessage($appGroupInfo['tx_code'],$messageData['content'],[$userInfoArray['tx_code']]);

                    // 发包人信息
                    $messageData['content'] = "{$receiveNickName}领取了您的红包！";
                    $messageData['group_tx_code'] = $appGroupInfo['tx_code'];
                    $messageData['tx_code'] = $sendUser['tx_code'];
                    $imModel->sendGroupMessage($appGroupInfo['tx_code'], $messageData);
//                    $imModel->sendGroupSingleSystemMessage($appGroupInfo['tx_code'],$messageData['content'],[$sendUser['tx_code']]);
                } else  {
                    // 只需要发送一条消息即可
                    $messageData['content'] = "您成功领取了自己的红包！";
                    $messageData['group_tx_code'] = $appGroupInfo['tx_code'];
                    $messageData['tx_code'] = $userInfoArray['tx_code'];
                    $imModel->sendGroupMessage($appGroupInfo['tx_code'], $messageData);
//                    $imModel->sendGroupSingleSystemMessage($appGroupInfo['tx_code'],$messageData['content'],[$userInfoArray['tx_code']]);
                }


//                if ($surplusNum == 0) {
//                    // 红包领取完毕
//                    $messageData['content'] = "您的红包在".Functions::diffDateTime(date("Y-m-d H:i:s",(strtotime($redPacketInfo['create_time']))),date("Y-m-d H:i:s"))."内领完！";
//                    $messageData['group_tx_code'] = $appGroupInfo['tx_code'];
//                    $messageData['tx_code'] = $sendUser['tx_code'];
//                    $imModel->sendGroupMessage($appGroupInfo['tx_code'], $messageData);
//                }

            } else if ($redPacketInfo['type'] == Status::RED_PACKET_TYPE_USER) {
                // 1对1 红包
                if (!empty($sendFriend) && empty($sendFriend['mark'])) {
                    $sendNickName = $sendFriend['mark'];
                }

                if (!empty($receiveFriend) && !empty($receiveFriend['mark'])) {
                    $receiveNickName = $receiveFriend['mark'];
                }

                $messageData['message_type'] = (int) Status::MESSAGE_TYPE_GROUP_USER_RECEIVE_RED_PACKET;
                $messageData['red_packet_id'] = (int) $redPacketInfo['id'];
                $messageData['type'] = (int) Status::MESSAGE_TYPE_GROUP_USER_RECEIVE_RED_PACKET;
                $messageData['content'] = "";
                $messageData['group_tx_code'] = "";
                $messageData['tx_code'] = "";
                $messageData['time'] = date('Y-m-d H:i:s');
                // 领取人信息
                $messageData['tx_code'] = $userInfoArray['tx_code'];
                $messageData['content'] = "您已领取了{$sendNickName}发送的红包！";
                $imModel->sendUserMessage($redPacketInfo['tx_code'], $messageData, $userInfoArray['tx_code']);

                // 发包人信息
                $messageData['tx_code'] = $sendUser['tx_code'];
                $messageData['content'] = "{$receiveNickName}成功领取了您发送的红包！";
                $imModel->sendUserMessage($userInfoArray['tx_code'], $messageData, $redPacketInfo['tx_code']);
            } else {
                // 专属红包
                if (!empty($sendFriend) && empty($sendFriend['mark'])) {
                    $sendNickName = $sendFriend['mark'];
                }

                if (!empty($receiveFriend) && !empty($receiveFriend['mark'])) {
                    $receiveNickName = $receiveFriend['mark'];
                }

                $groupId = $redPacketInfo['group_id'];
                $appGroupInfo = Group::getOneByWhere(['id' => $groupId]);
                // 领取人信息
                $messageData['content'] = "您成功领取了{$sendNickName}的专属红包！";
                $messageData['group_tx_code'] = $appGroupInfo['tx_code'];
                $messageData['tx_code'] = $userInfoArray['tx_code'];
                $imModel->sendGroupMessage($appGroupInfo['tx_code'], $messageData);

                // 发包人信息
                $messageData['content'] = "{$receiveNickName}领取了您的的专属红包！";
                $messageData['group_tx_code'] = $appGroupInfo['tx_code'];
                $messageData['tx_code'] = $sendUser['tx_code'];
                $imModel->sendGroupMessage($appGroupInfo['tx_code'], $messageData);
            }



        } catch (Exception $e) {
            file_put_contents('/tmp/redpacket_message.txt',print_r($e->getMessage(),true),FILE_APPEND);
        }
    }


}